package com.feng.spark.ml;

import java.io.Serializable;

public class NpArray implements Serializable{
	private static final long serialVersionUID = 1L;
	private Double []data;
	public NpArray(Double []data){
		this.data=data;
	}
	
	public NpArray(String []data){
		int len=data.length;
		this.data=new Double[len];
		for(int i=0;i<len;i++){
			this.data[i]=Double.valueOf(data[i]);
		}
	}
	
	public Integer getLength(){
		return this.data.length;
	}
	
	public Double[] getData(){
		return this.data;
	}
	
	public NpArray add(NpArray b) throws Exception{
		Double []bData=b.getData();
		Double []data=getData();
		bData[1].doubleValue();
		Integer bLen=b.getLength();
		if(bLen!=getLength()){
			throw new Exception("length no equal!");
		}
		//Double []sumData=new Double[bLen];
		for(int i=0;i<bLen;i++){
			data[i]=data[i]+bData[i];
		}
		return this;
	}
	
	public Double distance(NpArray b) throws Exception{
		Double []bData=b.getData();
		Double []data=getData();
		bData[1].doubleValue();
		Integer bLen=b.getLength();
		if(bLen!=getLength()){
			throw new Exception("length no equal!");
		}
		Double sum = 0.0;
		for (int i = 0; i < bLen; i++) {
			sum += (data[i] - bData[i]) * (data[i] - bData[i]);
		}
		return sum;
	}
	
	public NpArray divide(Integer b){
		int len=getLength();
		Double []data=getData();
		//Double []divideData=new Double[len];
		for(int i=0;i<len;i++){
			data[i]=(double)(data[i]/b);
		}
		return this;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder().append("[");
		int len=getLength();
		Double []data=getData();
		for (int i = 0; i < len; i++) {
			sb.append("field" + i + ":" + data[i]);
			if (i != len - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		return sb.toString();
	}
}
